package ngat.oss;

import ngat.message.OSS.*;
import ngat.astrometry.*;
import ngat.util.logging.*;
import ngat.phase2.*;
import ngat.instrument.*;

import java.io.*;

/** 
 * Action: Sets the current configuration and other settings for the 
 * specified Instrument for the Scheduling algorithm to use. Also indicates
 * whether the instrument is currently the selected instrument.
 *
 * $Id: INSTRUMENT_INFOImpl.java,v 1.1 2006/11/27 08:45:00 snf Exp $
 */
public class INSTRUMENT_INFOImpl extends TransactionImpl implements Logging {

    /** The new config to use for the specified Instrument.*/
    protected InstrumentConfig config;

    /** The Instrument's name/id.*/
    protected String instName;

    /** True if the selected instrument is currently in use.*/
    protected boolean current;

    /** The current status of the Instrument. The values are determined from
     * constants in ngat.message.OSS.INSTRUMENT_INFO.*/
    protected int status;

    /** The current operational status of the instrument.*/
    protected int opStatus;

    /** Create an INSTRUMENT_INFOImpl for the supplied request.*/ 
    public INSTRUMENT_INFOImpl(INSTRUMENT_INFO request) {
	super(request);
	this.config   = request.getConfig();
	this.instName = request.getInstName();
	this.current  = request.getCurrent();
	this.status   = request.getStatus();
	this.opStatus = request.getOperationalStatus();
    }

    /**
     * Set the specified Instrument's current configuration and status to those specified.
     * @return A Boolean or an OSS_Error.
     */
    public TRANSACTION_DONE exec() {

	logger.log(INFO, 1, "INSTRUMENT_INFO_Impl", id, "exec",
		   "Received command: "+request);

	Instrument inst = TelescopeStatus.getInstrument(instName);
	if (inst == null)
	    return error(INSTRUMENT_INFO.NO_SUCH_INSTRUMENT,
			 "Instrument name not recognized: "+instName); 
	//NO_SUCH_INSTR
	if (current) {
	    TelescopeStatus.setCurrentInstrument(inst);
	}
	
	try {
	    inst.configure(config);
	} catch (Exception ex) {
	   return error(INSTRUMENT_INFO.UNSUITABLE_CONFIG,
				"Failed to configure Inst: "+inst.getName()+
				" with config: "+config+" : "+ex);
	   
	}
	inst.setStatus(status);
	inst.setOperationalStatus(opStatus);

	logger.log(INFO, 1, "INSTRUMENT_INFO_Impl", id, "exec",
		   "Updated "+(current ? "(currently selected) " : "")+
		   "Instrument ("+instName+") info to:"+		 
		   "\n\tConfig:    "+config.toString()+
		   "\n\tNetStatus: "+status+"/"+Instrument.toNetworkStatusString(inst.getStatus())+
		   "\n\tOpStatus:  "+opStatus+"/"+Instrument.toOperationalStatusString(inst.getOperationalStatus()));
	
	INSTRUMENT_INFO_DONE done = new INSTRUMENT_INFO_DONE(id); 
	done.setSuccessful(true);
	return done;
    }
    
}

/** $Log: INSTRUMENT_INFOImpl.java,v $
/** Revision 1.1  2006/11/27 08:45:00  snf
/** Initial revision
/**
/** Revision 1.1  2006/11/27 08:37:05  snf
/** Initial revision
/** */
